#!/usr/local/bin/python3.7
import pymysql
import time
import sys
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

argv_path = sys.argv[0]
if ((os.path.isfile(argv_path))):
    print(argv_path + ' 已找到，开始执行...')
else:
    print('File not exist!!!')
    sys.exit()

startTime = time.time()  # 计时开始
print('计时开始，当前时间为', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(startTime)))

def login():
    loginUrl = 'https://mypartners.xm.com/#/login'
    # 加载界面
    driver.get(loginUrl)
    driver.delete_all_cookies()
    print('进入登录页面等待1分钟...')
    time.sleep(60)
    
    cookie_str = ''
    session = ''
    cookie_items = driver.get_cookies()
    for item_cookie in cookie_items:
        item_str = item_cookie["name"]+"="+item_cookie["value"]+"; "
        cookie_str += item_str
        if item_cookie["name"] == 'session':
            session = item_cookie["value"]
            print('session：',item_cookie["value"])
    driver.delete_all_cookies()
    username = driver.find_element('id','inputID')
    password = driver.find_element('id','inputPass')
    loginButton = driver.find_element('xpath','//*[@id="header-page"]/div[1]/div[2]/router-view/div[1]/form/loading-button/button')
   
    username.send_keys('A1021769')
    password.send_keys('abcabc123')
    loginButton.click()
    print('执行登录等待1分钟...')
    time.sleep(60)
    #获取cookie
    cookie_str = ''
    session = ''
    cookie_items = driver.get_cookies()
    #组装cookie字符串
    for item_cookie in cookie_items:
        item_str = item_cookie["name"]+"="+item_cookie["value"]+"; "
        cookie_str += item_str
        if item_cookie["name"] == 'session':
            session = item_cookie["value"]
            print('session：',item_cookie["value"])
    
    siteUrl = 'https://mypartners.xm.com/#/login'
    # 加载界面
    driver.get(siteUrl)
    print('进入首页等待10s...')
    time.sleep(10)
    
    response = ''
    try:
        response = driver.find_element('xpath','/html/body/div/div[2]/div/div/div/account-dropdown-menu-root-element/button')
        print('response',response)
    except:
        print('获取数据失败....')
    
    if response:
        #获取cookie
        cookie_str = ''
        session = ''
        cookie_items = driver.get_cookies()
        #组装cookie字符串
        for item_cookie in cookie_items:
            item_str = item_cookie["name"]+"="+item_cookie["value"]+"; "
            cookie_str += item_str
            if item_cookie["name"] == 'session':
                session = item_cookie["value"]
                print('session：',item_cookie["value"])
    
        # 数据库连接
        db = pymysql.connect(host='127.0.0.1',
                                 user='kufx',
                                 password='rByYNw5NcPWAmTGY',
                                 database='kufx')
    
        # 使用 cursor() 方法创建一个游标对象 cursor
        cursor = db.cursor()
    
        # SQL 更新语句
        sql = "UPDATE ht_set SET `value` = '" + session + "' WHERE `key` = 'cookie'"
        try:
           # 执行SQL语句
           cursor.execute(sql)
           # 提交到数据库执行
           db.commit()
        except:
           # 发生错误时回滚
           db.rollback()
    
        # 关闭数据库连接
        db.close()

    #打印出来看一下
    file = open('cookie.txt','w')
    file.write(cookie_str)
    file.close()
    driver.close()

# 创建chrome浏览器驱动，无头模式
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("--start-maximized")
#chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")  # 前面设置的端口号
#driver = webdriver.Chrome(
#    r'/Users/lizongjun/Downloads/chromedriver', options=chrome_options)

service = Service("/usr/bin/chromedriver")
driver = webdriver.Chrome(options=chrome_options,service=service)

login()


